package com.chuangsi.framework.web.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangsi.common.constant.HttpStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;

/**
 * 返回类
 * @param <T>
 */
public class ResultObject<T> {

    public static final String SUCCESS_MSG = "操作成功";
    public static final String ERROR_MSG = "操作失败";

    @ApiModelProperty("返回码")
    private int code;

    @ApiModelProperty("返回描述")
    private String msg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty("返回数据")
    private T data;

    @ApiModelProperty("总数")
    private long count;

    public static ResultObject ok(){
        return new ResultObject(HttpStatus.SUCCESS,SUCCESS_MSG);
    }

    public static ResultObject ok(String msg){
        ResultObject resultObject =new ResultObject(HttpStatus.SUCCESS,msg);
        return resultObject;
    }

    public static ResultObject pageOk(IPage page) {
        ResultObject resultObject = new ResultObject(HttpStatus.SUCCESS, "查询成功");
        resultObject.setCount(page.getTotal());
        resultObject.setData(page.getRecords());
        return resultObject;
    }
    public static ResultObject add(boolean flag) {
        if (flag) {
            return new ResultObject(HttpStatus.SUCCESS, "新增成功");
        }
        return new ResultObject(HttpStatus.ERROR, "新增失败");
    }

    public static ResultObject update(boolean flag) {
        if (flag) {
            return new ResultObject(HttpStatus.SUCCESS, "修改成功");
        }
        return new ResultObject(HttpStatus.ERROR, "修改失败");
    }

    public static ResultObject delete(boolean flag) {
        if (flag) {
            return new ResultObject(HttpStatus.SUCCESS, "删除成功");
        }
        return new ResultObject(HttpStatus.ERROR, "删除失败");
    }

    public static ResultObject error(){
        ResultObject resultObject =new ResultObject(HttpStatus.ERROR,ERROR_MSG);
        return resultObject;
    }

    public static ResultObject error(String msg){
        ResultObject resultObject =new ResultObject(HttpStatus.ERROR,msg==null?"接口调用失败":msg);
        return resultObject;
    }

    public ResultObject() {
        this.code=HttpStatus.SUCCESS;
        this.msg=SUCCESS_MSG;
    }

    public ResultObject(int code){
        this();
        this.code =code;
    }

    public ResultObject(int code, String msg){
        this(code);
        this.msg= msg;
    }

    public ResultObject(int code, String msg, T data) {
        this(code,msg);
        this.data = data;
    }

    public ResultObject<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public ResultObject<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public ResultObject<T> setData(T data) {
        this.data = data;
        return this;
    }

    public ResultObject<T> setCount(long count){
        this.count = count;
        return this;
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public long getCount() {
        return this.count;
    }

    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof ResultObject)) return false;
        final ResultObject<?> other = (ResultObject<?>) o;
        if (!other.canEqual((Object) this)) return false;
        final Object this$code = this.getCode();
        final Object other$code = other.getCode();
        if (this$code == null ? other$code != null : !this$code.equals(other$code)) return false;
        final Object this$msg = this.getMsg();
        final Object other$msg = other.getMsg();
        if (this$msg == null ? other$msg != null : !this$msg.equals(other$msg)) return false;
        final Object this$data = this.getData();
        final Object other$data = other.getData();
        if (this$data == null ? other$data != null : !this$data.equals(other$data)) return false;
        if (this.getCount() != other.getCount()) return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof ResultObject;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $code = this.getCode();
        result = result * PRIME + ($code == null ? 43 : $code.hashCode());
        final Object $msg = this.getMsg();
        result = result * PRIME + ($msg == null ? 43 : $msg.hashCode());
        final Object $data = this.getData();
        result = result * PRIME + ($data == null ? 43 : $data.hashCode());
        final long $count = this.getCount();
        result = result * PRIME + (int) ($count >>> 32 ^ $count);
        return result;
    }

    public String toString() {
        return "ResultObject(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ", count=" + this.getCount() + ")";
    }
}
